﻿Option Explicit On
Option Strict On
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Namespace Framework

    Public NotInheritable Class GenPassWord
        'แปลงpassword ให้เป็นตัวหนอนที่อ่านค่าไม่ได้
        Public Shared Function Encripting(ByVal msg As String) As String
            Dim mykey As String = "codelove"
            Dim encryptedPassword As System.IO.MemoryStream = New System.IO.MemoryStream
            Dim des As System.Security.Cryptography.DESCryptoServiceProvider = New System.Security.Cryptography.DESCryptoServiceProvider
            des.Key = System.Text.Encoding.ASCII.GetBytes(mykey)
            Dim iv() As Byte = New Byte() {11, 12, 33, 50, 78, 25, 72, 84}
            des.IV = iv
            Dim myEncryptor As System.Security.Cryptography.ICryptoTransform = des.CreateEncryptor
            Dim myCryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(encryptedPassword, myEncryptor, System.Security.Cryptography.CryptoStreamMode.Write)
            Dim pwd() As Byte = System.Text.Encoding.ASCII.GetBytes(msg)
            myCryptoStream.Write(pwd, 0, pwd.Length)
            myCryptoStream.Close()
            'Dim tmp As String = Convert.ToBase64String(encryptedPassword.ToArray)
            'MsgBox(Convert.ToBase64String(encryptedPassword.ToArray))
            Return Convert.ToBase64String(encryptedPassword.ToArray)
        End Function

        Public Shared Function Decripting(ByVal msg As String) As String
            Dim decryptedPassword As System.IO.MemoryStream = New System.IO.MemoryStream
            Dim des As System.Security.Cryptography.DESCryptoServiceProvider = New System.Security.Cryptography.DESCryptoServiceProvider
            des.Key = System.Text.Encoding.ASCII.GetBytes("codelove")
            Dim iv() As Byte = New Byte() {11, 12, 33, 50, 78, 25, 72, 84}
            des.IV = iv
            Dim myDecryptor As System.Security.Cryptography.ICryptoTransform = des.CreateDecryptor
            Dim myCryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(decryptedPassword, myDecryptor, System.Security.Cryptography.CryptoStreamMode.Write)
            Dim encryptedPassword() As Byte = Convert.FromBase64String(msg)
            myCryptoStream.Write(encryptedPassword, 0, encryptedPassword.Length)
            myCryptoStream.Close()
            Return System.Text.Encoding.ASCII.GetString(decryptedPassword.ToArray)
        End Function
    End Class
End Namespace

